package com.glory.hikvisionmobile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.EqpKeepCheckListAdapter;
import com.glory.hikvisionmobile.adapter.EqpKeepInputListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepSelectorListBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Eqp_EqpKeepDetailsActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.rvKeepCheckList)
    RecyclerView rvKeepCheckList;
    @BindView(R.id.rvKeepInputList)
    RecyclerView rvKeepInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llInputLayout)
    LinearLayout llInputLayout;
    @BindView(R.id.tvSave)
    TextView tvSave;
    private String scheduleRrn;
    private EqpKeepCheckListAdapter eqpKeepCheckListAdapter;
    private EqpKeepInputListAdapter eqpKeepInputListAdapter;
    private List<EqpKeepSelectorListBean> checkTableLineDatas;
    private String eqpWork;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_keep_detials;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        scheduleRrn = bundle.getString("scheduleRrn");
        eqpWork = bundle.getString("eqpWork");

        rvKeepCheckList.setLayoutManager(new LinearLayoutManager(Eqp_EqpKeepDetailsActivity.this));
        rvKeepCheckList.setItemAnimator(new DefaultItemAnimator());
        rvKeepCheckList.addItemDecoration(new DividerItemDecoration(Eqp_EqpKeepDetailsActivity.this, 1));
        eqpKeepCheckListAdapter = new EqpKeepCheckListAdapter(null);
        eqpKeepCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepCheckListAdapter.isFirstOnly(true);
        rvKeepCheckList.setAdapter(eqpKeepCheckListAdapter);

        rvKeepInputList.setLayoutManager(new LinearLayoutManager(Eqp_EqpKeepDetailsActivity.this));
        rvKeepInputList.setItemAnimator(new DefaultItemAnimator());
        rvKeepInputList.addItemDecoration(new DividerItemDecoration(Eqp_EqpKeepDetailsActivity.this, 1));
        eqpKeepInputListAdapter = new EqpKeepInputListAdapter(null);
        eqpKeepInputListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepInputListAdapter.isFirstOnly(true);
        rvKeepInputList.setAdapter(eqpKeepInputListAdapter);

        eqpKeepCheckListAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "queryScheduleForm");
        map.addProperty("scheduleRrn", scheduleRrn);
        NetUtils.requestDara("HIK.PdaPmsSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>(
                new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpKeepBean eqpKeepBean = response.body().getBody();
                    if (eqpKeepBean.getCheckTableLineDatas().size() > 0){
                        llCheckLayout.setVisibility(View.VISIBLE);
                        checkTableLineDatas = eqpKeepBean.getCheckTableLineDatas();
                        eqpKeepCheckListAdapter.setNewData(checkTableLineDatas);
                    }

                    if (eqpKeepBean.getLineDatas().size() > 0){
                        llInputLayout.setVisibility(View.VISIBLE);
                        eqpKeepInputListAdapter.setNewData(eqpKeepBean.getLineDatas());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaPmsSchedule");
            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备保养");
        freeUI_titleView.setRightListener(R.drawable.selector_refresh, v -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    eqpKeepCheckListAdapter.getData().clear();
                    eqpKeepCheckListAdapter.notifyDataSetChanged();
                    eqpKeepInputListAdapter.getData().clear();
                    eqpKeepInputListAdapter.notifyDataSetChanged();
                    eqpKeepInputListAdapter.clearData();
                    initData();
                }
            });

        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.tvReplay,R.id.tvSave})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvSave:
                DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> submit("false"),getSupportFragmentManager());
                break;
            case R.id.tvReplay:
                initPermission("Submit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        if (eqpKeepInputListAdapter.getOutData().size() > 0) {
                            showConfirmDialog("数值已超限");
                            return;
                        }

                        List<EqpKeepSelectorListBean> data = eqpKeepCheckListAdapter.getData();
                        for (EqpKeepSelectorListBean bean : data){
                            if (FreeApi_StringUtils.isEmpty(bean.getReserved1())){
                                showToast("保养状态项不能为空",false);
                                return;
                            }

                            if (FreeApi_StringUtils.isEmpty(bean.getReserved9()) && bean.getReserved10().length() > 0 && !bean.getReserved10().contains(";")){
                                showToast("QC采集项未完成",false);
                                return;
                            }
                        }

                        DialogUtils.showLoginDialog(Eqp_EqpKeepDetailsActivity.this, Constant.CURRENT_USER, () -> submit("true"),getSupportFragmentManager());
                    }
                });
                break;
        }
    }

    private void submit(String isFinished) {
        JsonObject map = new JsonObject();
        JsonObject checkJson = new JsonObject();
        checkJson.add("XPdaPmsSheetCheckTableLineData",new JsonParser().parse(new Gson().toJson(eqpKeepCheckListAdapter.getData())).getAsJsonArray());
        JsonObject inputJson = new JsonObject();
        inputJson.add("XPdaPmsDatasLineData",new JsonParser().parse(new Gson().toJson(eqpKeepInputListAdapter.getData())).getAsJsonArray());
        map.addProperty("actionType", "saveScheduleForm");
        map.addProperty("isFinished", isFinished);
        map.add("XPdaPmsDatasLineDatas",inputJson);
        map.add("XPdaPmsSheetCheckTableLineDatas",checkJson);
        map.addProperty("scheduleRrn",scheduleRrn);
        NetUtils.requestDara("HIK.PdaPmsSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>(
                new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(Eqp_EqpKeepDetailsActivity.this,isFinished.equals("true") ? "保存成功" : "临时保存成功");

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaPmsSchedule");
            }
        });
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EqpKeepSelectorListBean item = checkTableLineDatas.get(position);
        if (item.getReserved10().length() > 0 && !item.getReserved10().contains(";") && !item.getReserved9().equals("Y")){
            if (actionControl("Work_DataCollectActivity","PDAWorkFragment","无一般数据采集权限")) {
                return;
            }
            Intent intent = new Intent(this,Work_DataCollectActivity.class);
            intent.putExtra("position",String.valueOf(position));
            intent.putExtra("eqpWork",eqpWork);
            intent.putExtra("paramList",item.getReserved10());
            startActivityForResult(intent,500);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == 500){
                if (data != null){
                    int position = Integer.parseInt(data.getStringExtra("position"));
                    if (position >= 0){
                        checkTableLineDatas.get(position).setReserved9("Y");
                        eqpKeepCheckListAdapter.notifyItemChanged(position);
                    }else {
                        showConfirmDialog("数据采集填写错误");
                    }

                }
            }
        }
    }
}
